if (!jsx3.lang.Class.forName("tibco.admin.enterpriseAssets.userMgt.user.Add")) {


    /**
     *
     */
    jsx3.lang.Class.defineClass("tibco.admin.enterpriseAssets.userMgt.user.Add",
            jsx3.gui.LayoutGrid,
            null,
            function (add) {

                add.prototype.initialize = function() {
                    // set the window instance
                    if (jsx3.lang.Class.forName("tibco.admin.enterpriseAssets.userMgt.window.Window")) {
                        // set the window instance
                        this.window = this.getAncestorOfType(tibco.admin.enterpriseAssets.userMgt.window.Window)
                        if (this.window) {
                            this.setWindowId(this.window.getId());
                        }
                    }

                    this.passedItems["userId"] = false;
                    //070818
                    this.passedItems["pwd"] = false;
                    //070818
                    this.passedItems["confirmPwd"] = false;
                    //070818
                }

                add.prototype.setWindowId = function(strId) {
                    this.windowId = strId
                }


                add.prototype.getWindowId = function() {
                    return this.windowId
                }

                //call this method to begin the service call (eg.service.calladdUser();)
                add.prototype.calladdUser = function() {
                    if (this.PwdMatch()) {
                        this.adduservalidate = true
                        tibco.admin.util.Log.USERMGT.trace("tibco.admin.enterpriseAssets.userMgt.user.Add#calladdUser")
                        var objService = tibco.admin.Main.loadService("tibco.admin", "tibco/admin/enterpriseAssets/userMgt/window/user/rules/addUser.xml", "addUser", "Add Users");

                        //subscribe and call
                        objService.subscribe(jsx3.net.Service.ON_SUCCESS, this, this.onaddUserSuccess);
                        objService.subscribe(jsx3.net.Service.ON_ERROR, this, this.onaddUserError);
                        objService.subscribe(jsx3.net.Service.ON_INVALID, this, this.onaddUserInvalid);
                        objService.setEndpointURL(tibco.admin.util.ServiceHelper.getUserMgtEndpointURL());
                        objService.username = this.getDescendantOfName("tbxUserID_24_C_Add").getValue();
                        objService.getServiceMessage();
                        if (this.adduservalidate) {
                            var errorCode = this.confirmPsw();
                            if (errorCode == 0) {
                                objService.doCall(true);
                            }
                            else {
                                this.adduservalidate = false
                                this.getDescendantOfName("blkServerMessages_24_C_Add").setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true)
                                if (errorCode == 1)
                                    this.getDescendantOfName("lblMessage_24_C_Add").
                                            setText(tibco.admin.Main.getServer().getDynamicProperty("@@inputValidPassword"), true);

                                else  if (errorCode == 2)
                                    this.getDescendantOfName("lblMessage_24_C_Add").
                                            setText(tibco.admin.Main.getServer().getDynamicProperty("@@passwordNotMatch"), true);
                            }
                        }
                    }
                };

                add.prototype.onaddUserSuccess = function(objEvent) {
                    tibco.admin.util.Log.USERMGT.trace("tibco.admin.enterpriseAssets.userMgt.user.Add#onaddUserSuccess");
                    //jsx3.log("onaddUserSuccess " + objEvent.target.simpleActionResponse);
                    tibco.admin.util.Log.USERMGT.trace("objEvent.target.simpleActionResponse ")
                    if (objEvent.target.simpleActionResponse == "true") {
                        this.getDescendantOfName("blkServerMessages_24_C_Add").setDisplay(jsx3.gui.Block.DISPLAYNONE, true)
                        this.resetPasswordFieled()
                        // Publish a message to window
                        var msg = new Object();
                        msg.subject = tibco.admin.enterpriseAssets.userMgt.EventID.ADD_ITEM_SUCCESS
                        msg.windowId = this.getWindowId();
                        msg.username = objEvent.target.username;
                        tibco.admin.PubSub.publish(msg);
                    }
                    else {
                        this.getDescendantOfName("blkServerMessages_24_C_Add").setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true)
                        this.getDescendantOfName("lblMessage_24_C_Add").
                                setText(tibco.admin.Main.getServer().getDynamicProperty("@@faildAddUser"), true);
                    }

                    this.setMasterRefreshEnabled();
                };

                add.prototype.onaddUserError = function(objEvent) {
                    this.getDescendantOfName("blkServerMessages_24_C_Add").setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true)
                    this.getDescendantOfName("lblMessage_24_C_Add").
                            setText(tibco.admin.Main.getServer().getDynamicProperty("@@serverError"), true);

                };

                add.prototype.onaddUserInvalid = function(objEvent) {
                    this.adduservalidate = false;
                    this.getDescendantOfName("blkServerMessages_24_C_Add").setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true)
                    this.getDescendantOfName("lblMessage_24_C_Add").
                            setText(tibco.admin.Main.getServer().getDynamicProperty("@@namingError"), true);
                };

                add.prototype.cancelAddUser = function(master) {
                    this.getDescendantOfName("blkServerMessages_24_C_Add").setDisplay(jsx3.gui.Block.DISPLAYNONE, true)
                    this.resetPasswordFieled()
                    // Publish a message to window
                    var msg = new Object();
                    msg.subject = tibco.admin.enterpriseAssets.userMgt.EventID.ADD_ITEM_CANCELED
                    msg.master = master
                    msg.windowId = this.getWindowId();
                    tibco.admin.PubSub.publish(msg);

                    this.setMasterRefreshEnabled();
                }

                /**
                 * Check whether the password and confirm password are the same
                 *    ERROR_CODE   0: valid
                 *                        1: password length &lt; 5
                 *	 					   2: password and confirm password not match
                 **/
                add.prototype.confirmPsw = function() {
                    var psswd = this.getDescendantOfName("tbxPassword_24_C_Add").getValue()
                    var confirm = this.getDescendantOfName("tbxCfmPassword_24_C_Add").getValue()
                    if (psswd.length < 1) {
                        return 1;
                    } else {
                        if (psswd == confirm) {
                            return 0;
                        }
                        else {
                            return 2;
                        }
                    }
                }

                add.prototype.resetPasswordFieled = function() {
                    this.getDescendantOfName("tbxUserID_24_C_Add").setValue("").repaint()
                    this.getDescendantOfName("tbxPassword_24_C_Add").setValue("").repaint()
                    this.getDescendantOfName("tbxCfmPassword_24_C_Add").setValue("").repaint()
                };

                add.prototype.setMasterRefreshEnabled = function() {
                    var msg = new Object();
                    msg.subject = tibco.admin.enterpriseAssets.userMgt.EventID.USER_ENABLED_REFRESH;
                    tibco.admin.PubSub.publish(msg);
                }


                /*070818
                    * if user doesn't fishish all the required fields, the "save" button will be disabled.
                    */
                add.prototype.passedItems = {};

                add.prototype.valiUserId = function(obj, strValue) {
                    this.passedItems["userId"] = tibco.admin.util.Validation.doValidateNotEmptyNoWhSpace(obj, strValue, tibco.admin.Main.getServer().getDynamicProperty("@@comUserId_24"));
                    this.enableSaveBtn4Add();
                };

                add.prototype.valiPwd = function(obj, strValue) {
                    this.passedItems["pwd"] = tibco.admin.util.Validation.doValidateNotEmptyNoWhSpace(obj, strValue, tibco.admin.Main.getServer().getDynamicProperty("@@lblPassword_24_C"));
                    this.enableSaveBtn4Add();
                };

                add.prototype.valiConfirmPwd = function(obj, strValue) {
                    this.passedItems["confirmPwd"] = tibco.admin.util.Validation.doValidateNotEmptyNoWhSpace(obj, strValue, tibco.admin.Main.getServer().getDynamicProperty("@@lblCfmPassword_24_C"));
                    this.enableSaveBtn4Add();
                };

                add.prototype.enableSaveBtn4Add = function() {
                    var allPassed = true;
                    for (var item in this.passedItems) {
                        if (!this.passedItems[item]) allPassed = false;
                    }
                    if (allPassed) this.getDescendantOfName("btnSave_24_C_Add").setEnabled(jsx3.gui.Form.STATEENABLED, true);
                    else this.getDescendantOfName("btnSave_24_C_Add").setEnabled(jsx3.gui.Form.STATEDISABLED, true);
                }
                add.prototype.PwdMatch = function() {
                    var flag = true;
                    if (this.passedItems["pwd"] && this.passedItems["confirmPwd"]) {
                        var passwordField = this.getDescendantOfName("tbxPassword_24_C_Add");
                        var confirmPasswordField = this.getDescendantOfName("tbxCfmPassword_24_C_Add");
                        if (passwordField.getValue() != confirmPasswordField.getValue()) {
                            var notMatchTxt = tibco.admin.Main.getServer().getDynamicProperty("@@passwordNotMatch");
                            tibco.admin.util.Validation.actionOnValidation(confirmPasswordField, false, notMatchTxt);
                            flag = false;
                        }  else {
                            tibco.admin.util.Validation.actionOnValidation(confirmPasswordField, true);
                        }
                    }
                    return flag;
                }

            });
}